using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Data;
using Widgetsphere.Core.DataAccess;

namespace Acme.AuditExample.Business.Audit
{
	/// <summary>
	 /// The object to hold the 'CustomerAudit' entity
	/// </summary>
	public partial class CustomerAudit : IAudit
	{
		#region Constructors

		internal CustomerAudit()
		{
		}

		#endregion

		#region Properties

		/// <summary>
		/// The property that maps back to the database 'Customer.CustomerId' field
		/// </summary>
		public System.Guid CustomerId { get; internal set; }
		/// <summary>
		/// The property that maps back to the database 'Customer.Name' field
		/// </summary>
		public string Name { get; internal set; }
		/// <summary>
		/// The type of audit
		/// </summary>
		public AuditTypeConstants AuditType { get; internal set; }
		/// <summary>
		/// The date of the audit
		/// </summary>
		public DateTime AuditDate { get; internal set; }
		/// <summary>
		/// The modifier value of the audit
		/// </summary>
		public string ModifiedBy { get; internal set; }

		#endregion

		/// <summary>
		/// Gets a set of audit records based on a primary key
		/// </summary>
		/// <returns>A set of audit records for the current record based on primary key</returns>
		public static IEnumerable<Acme.AuditExample.Business.Audit.CustomerAudit> GetValues(System.Guid customerId)
		{
			return GetValues(0, 0, null, null, customerId).InnerList;
		}

		/// <summary>
		/// Gets a set of audit records based on a primary key
		/// </summary>
		/// <param name="pageOffset">The page offset needed for pagination starting from page 1</param>
		/// <param name="recordsPerPage">The number of records to be returned on a page.</param>
		/// <param name="startDate">The starting date used when searching for records.</param>
		/// <param name="endDate">The ending date used when searching for records.</param>
		/// <returns>A set of audit records for the current record based on primary key</returns>
		public static AuditPaging<CustomerAudit> GetValues(int pageOffset, int recordsPerPage, DateTime? startDate, DateTime? endDate, System.Guid customerId)
		{
			AuditPaging<Acme.AuditExample.Business.Audit.CustomerAudit> retval = new AuditPaging<Acme.AuditExample.Business.Audit.CustomerAudit>();
			retval.PageOffset = pageOffset;
			retval.RecordsPerPage = recordsPerPage;
			List<Acme.AuditExample.Business.Audit.CustomerAudit> innerList = new List<Acme.AuditExample.Business.Audit.CustomerAudit>();

			IDataReader dataReader = null;
			IDbConnection dbConnection = null;
			IDbCommand dbCommand = null;

			try
			{
				dbConnection = DatabaseHelper.GetConnection(ConfigurationValues.GetInstance().ConnectionString);
				dbConnection.Open();

				dbCommand = DatabaseHelper.GetCommand(ConfigurationValues.GetInstance().ConnectionString, "[dbo].[gen_Customer__AUDIT_SELECT]");
				DatabaseHelper.SetParameterValue(dbCommand, "@__pageOffset", pageOffset);
				if (pageOffset != 0 && recordsPerPage != 0)
					DatabaseHelper.SetParameterValue(dbCommand, "@__recordsPerPage", recordsPerPage);
				DatabaseHelper.SetParameterValue(dbCommand, "@__startDate", startDate);
				DatabaseHelper.SetParameterValue(dbCommand, "@__endDate", endDate);
				DatabaseHelper.SetParameterValue(dbCommand, "@CustomerId", customerId);
				dbCommand.Connection = dbConnection;
				dataReader = dbCommand.ExecuteReader();

				// Fill the list box with the values retrieved
				int ordinal = 0;
				while (dataReader.Read())
				{
					CustomerAudit si = new CustomerAudit();

					ordinal = dataReader.GetOrdinal("__action");
					si.AuditType = (AuditTypeConstants)dataReader.GetInt32(ordinal);

					ordinal = dataReader.GetOrdinal("__insertdate");
					si.AuditDate = dataReader.GetDateTime(ordinal);

					ordinal = dataReader.GetOrdinal("ModifiedBy");
					si.ModifiedBy = dataReader.IsDBNull(ordinal) ? null : dataReader.GetString(ordinal);

					ordinal = dataReader.GetOrdinal("CustomerId");
					si.CustomerId = dataReader.GetGuid(ordinal);

					ordinal = dataReader.GetOrdinal("Name");
					si.Name = dataReader.IsDBNull(ordinal) ? (string)null : dataReader.GetString(ordinal);

					innerList.Add(si);
				}
				retval.InnerList = innerList;
			}
			catch (Exception ex)
			{
				throw;
			}
			finally
			{
				if (dataReader != null) dataReader.Close();
				retval.TotalRecordCount = (int)((System.Data.IDbDataParameter)(dbCommand.Parameters["@RETURN_VALUE"])).Value;
				if (dbConnection.State == ConnectionState.Open) dbConnection.Close();
			}
			return retval;

		}

		private static byte[] ReadFromByteArray(int ordinal, IDataReader dataReader)
		{
			int bufferSize = 1024 * 10;
			int startIndex = 0;
			byte[] outbyte = new byte[bufferSize];
			List<byte> retval = new List<byte>();

			// Read the bytes into outbyte[] and retain the number of bytes returned.
			long count = dataReader.GetBytes(ordinal, startIndex, outbyte, 0, bufferSize);

			// Continue reading and writing while there are bytes beyond the size of the buffer.
			while (count == bufferSize)
			{
				retval.AddRange(outbyte);
				startIndex += bufferSize;
				count = dataReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
			}
			return retval.ToArray();
		}

	}

}
